package cn.hzking.gjmall.net.base;

import android.app.Activity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.fingdo.statelayout.StateLayout;

import cn.hzking.gjmall.R;
import cn.hzking.gjmall.utils.UIUtils;

/**
 * Created by Administrator on 2018/1/29.
 */

public  abstract class GjObserver<T extends BaseEntity> extends BaseObserver<T> {
    private StateLayout stateLayout;
    private boolean isFistInit;
    private boolean isRefresh;
    private SwipeRefreshLayout swipeRefreshLayout;

    protected abstract void onBusinessSuccess(T t) throws Exception;//业务逻辑成功
    protected abstract void onChildFailure(Throwable e, boolean isNetWorkError) throws Exception;//请求失败
    //请求结束
    protected void  onChildRequestEnd(){

    }
    protected  void onChildRequestStart(){

    }
    protected   void onBusinessFail(T t){

    }


    public GjObserver(Activity cxt, StateLayout stateLayout, SwipeRefreshLayout swipeRefreshLayout ,boolean isFistInit,boolean isRefresh) {
        super(cxt,true,false);
        this.stateLayout = stateLayout;
        this.isFistInit = isFistInit;
        this.swipeRefreshLayout = swipeRefreshLayout;
        this.isRefresh = isRefresh;
    }
    public GjObserver(Activity cxt, StateLayout stateLayout, SwipeRefreshLayout swipeRefreshLayout, boolean isFistInit,boolean isRefresh,boolean isShowDialog,boolean isCancel) {
        super(cxt,isShowDialog,isCancel);
        this.stateLayout = stateLayout;
        this.isFistInit = isFistInit;
        this.isRefresh = isRefresh;
        this.swipeRefreshLayout = swipeRefreshLayout;
    }


    //请求开始
    @Override
    protected void onRequestStart() {
        super.onRequestStart();
        onChildRequestStart();

    }

    //请求成功(result = success)
    @Override
    protected void onSuccees(T t) throws Exception {
        stateLayout.showContentView();
        onBusinessSuccess(t);
        //做具体的业务逻辑操作



    }

    //业务逻辑请求失败(result = fail)
    @Override
    protected void onCodeError(T t) throws Exception {
        super.onCodeError(t);
        if (isFistInit) {
            stateLayout.showEmptyView();
        } else {
            swipeRefreshLayout.setRefreshing(false);
        }
        onBusinessFail(t);
    }

    //请求失败
    @Override
    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
        //当加载错误的时候
        if (isFistInit) {//是第一次加载数据
            View customView = LayoutInflater.from(mActivity).inflate(R.layout.layout_statelayout_custom_error1, null);
            // stateLayout.showEmptyView();
            stateLayout.showCustomView(customView);
          //  stateLayout.showEmptyView();
        } else {//是上拉加载更多或者是数据刷新
            swipeRefreshLayout.setRefreshing(false);
            if(isNetWorkError) {
                UIUtils.showToast(mActivity, "网络不给力", Toast.LENGTH_SHORT);
            }else {
                UIUtils.showToast(mActivity, "服务器错误", Toast.LENGTH_SHORT);
            }
        }
        onChildFailure(e ,isNetWorkError);

    }

    //请求完成
    @Override
    protected void onRequestEnd() {
        super.onRequestEnd();
        onChildRequestEnd();
        if(isRefresh) {
            swipeRefreshLayout.setRefreshing(false);
        }
    }
}
